<?php

require_once "Zend/Tokenizer/String/Abstract.php";

/**
 * @category   Zend
 * @package    Zend_Tokenizer
 * @copyright  Salomon BRYS
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 *
 * This tokenizer gives tokens representing a line from the stream.
 */
class Zend_Tokenizer_String_Line extends Zend_Tokenizer_String_Abstract
{
	private $_ignoreEmpty = false;

	/**
	 * Constructor
	 *
	 * @param Zend_Tokenizer_Interface $producer
	 */
	public function __construct(Zend_Tokenizer_Interface $producer, $ignoreEmpty = false)
	{
		parent::__construct($producer);

		$this->_ignoreEmpty = $ignoreEmpty;
	}

	/**
	 * return wether there's at least one token ready.
	 *
	 * @return bool
	 */
	protected function checkHasToken($data)
	{
		return (strpos($data, "\n") !== false);
	}

	/**
	 * Return the next token.
	 *
	 * @return mixed
	 */
	protected function extractFirstToken(&$data)
	{
		$data = ltrim($data, "\r\n");
		$array = explode("\n", $data, 2);
		if (isset($array[1])) {
			$data = $array[1];
		} else {
			$data = "";
		}
		return rtrim($array[0], "\r");
	}

	/**
	 * Removes empty lines if required
	 * @see Zend_Tokenizer/library/Zend/Tokenizer/String/Zend_Tokenizer_String_Abstract::_fillData()
	 */
	protected function _fillData()
	{
		parent::_fillData();
		if ($this->_ignoreEmpty) {
			$this->_data = ltrim($this->_data, "\r\n");
		}
	}
}
